let url=document.location.href;
let articleId = url.substring(url.indexOf("=") + 1);
let count=0;

$("#load-more").click(function () {
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.status === 200 && xhr.readyState === 4) {
            let s=xhr.responseText;
            if(s.length===0){
                $("#load-more").text("没有更多了...");
                return;
            }
            let t = JSON.parse(s);
            let blogs=t.blogs;
            addSomething(blogs);
        }
    };
    xhr.open("GET", "./person_more_page?userId=" + articleId + "&n=" + count);
    xhr.send();
    count++;
});

$(function () {

    let id=window.location.href.split("userId=")[1];
    let cookie = $.cookie("id");
    if(cookie===id){
        $("#newBlog").append("<a href=\"../blog/blog_edit?userId="+id+"\">\n" +
            "                            <label style=\"color: whitesmoke;font-weight: normal;border: 10px solid lightcoral;border-radius: 10px;background-color: lightcoral\">写文章</label>\n" +
            "                        </a>")
    }

    let xhr=new XMLHttpRequest();

    xhr.onreadystatechange = function () {
        if (xhr.status === 200 && xhr.readyState === 4) {
            let s=xhr.responseText;
            let t = JSON.parse( s);

            $("#username").html("<i class=\"fa fa-user\"></i>" + t.name);

            let allMyBlogs=t.allMyBlogs;
            addSomething(allMyBlogs);

            let category=t.category;
            for(let i=0;i<category.length;i++){
                $("#category").append("<a href=\"#\">"+ category[i].tag+"<span>("+ category[i].tally+")</span></a>");
            }
            let hottestBlogs=t.hottestBlogs;
            for(let i=0;i<hottestBlogs.length;i++){

                // $("#hottest_blogs").append("<div class=\"widget-post\"><a href=\"#\"><span style='font-weight: lighter;font-size: large'>"+hottestBlogs[i].firstTitle+"</span></a><ul class=\"blog-meta\"><li>"+hottestBlogs[i].time+"</li></ul></div>");
                $("#hottest_blogs").append("<div class=\"widget-post\"><a href=\"#\"><span style='font-weight: normal;font-size: large'>"+"筑梦第八次全梦代表大会顺利召开筑梦第八次全梦代表大会顺利召开筑梦第八次全梦代表大会顺利召开筑梦第八次全梦代表大会顺利召开"+"</span></a><ul class=\"blog-meta\"><li>"+hottestBlogs[i].time+"</li></ul></div>");
            }

            let currentBlogs=t.currentBlogs;
            for(let i=0;i<currentBlogs.length;i++){
                // $("#current_blogs").append("<div class=\"widget-post\"><a href=\"#\"><span style='font-size: large;font-weight: lighter'>"+currentBlogs[i].firstTitle+"</span></a><ul class=\"blog-meta\"><li>"+currentBlogs[i].time+"</li></ul></div>");
                $("#current_blogs").append("<div class=\"widget-post\"><a href=\"#\"><span style='font-size: large;font-weight: normal'>"+"筑梦实验室关于如何培养国家软件方面领军人才的方案"+"</span></a><ul class=\"blog-meta\"><li>"+currentBlogs[i].time+"</li></ul></div>");
            }
        }
    };

    xhr.open("GET", "./person_page?userId=" + articleId);
    xhr.send();
    count++;
});

function addSomething(blogs) {

    for(let i=0;i<blogs.length;i++){
        let temp = "<ul>\n" +
            "                        <a href=../blog/blog-single.jsp?article_id="+ blogs[i].id +" style=\"color: black\">\n" +
            "                            <li class=\"blogs\">\n" +
            "                                <h3 class='firstTitle'>" + blogs[i].firstTitle + "</h3>\n" +
            "                                <p class='secondTitle'>" + blogs[i].secondTitle + "</p>\n" +
            "                                <p align=\"right\"><i class=\"fa fa-clock-o\" aria-hidden=\"true\" style=\"color: #1A8BE8\"></i>" + blogs[i].date + "</p>\n" +
            "                            </li><br/></a>\n" +
            "                    </ul>";
        $("#blog_list").append(temp);
    }

}